﻿Imports System.Data.OleDb
Imports System.Xml
Imports System.ServiceModel.Syndication
Partial Public Class rss
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.ContentType = "application/rss+xml"

        Dim myFeed As New SyndicationFeed()

        myFeed.Title = SyndicationContent.CreatePlaintextContent("Riyadh Resturants")
        myFeed.Description = SyndicationContent.CreatePlaintextContent("Last added resturants")
        myFeed.Links.Add(SyndicationLink.CreateSelfLink(New Uri(getfullURL("Default.aspx"))))
        myFeed.Language = "en-us"


        Dim oleDb As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("RUHRestaurantsDB").ConnectionString)
        oleDb.Open()

        Dim SqlString As String = "SELECT TOP 5 * FROM Resturants ORDER BY Id DESC"

        Dim dbCom As OleDbCommand = New OleDbCommand(SqlString, oleDb)
        dbCom.CommandType = CommandType.Text

        Dim dbReader As OleDbDataReader = dbCom.ExecuteReader()

        Dim items As New List(Of SyndicationItem)
        While dbReader.Read()
            Dim item As New SyndicationItem()
            item.Title = mkRssTxt(dbReader.Item("RestName"))
            item.Summary = mkRssTxt("Resturant Name: " & dbReader.Item("RestName") & "<br> Address:" & dbReader.Item("Address"))
            item.Links.Add(mkRssLink(Request.Url.GetLeftPart(UriPartial.Authority) & "/Details.aspx?id=" & dbReader.Item("Id")))

            items.Add(item)
        End While

        myFeed.Items = items

        Dim feedWritter As XmlWriter = XmlWriter.Create(Response.OutputStream)

        Dim rssFormatter = New Rss20FeedFormatter(myFeed)

        rssFormatter.WriteTo(feedWritter)

        feedWritter.Close()

    End Sub

    Private Function mkRssLink(ByVal str As String) As SyndicationLink
        Return SyndicationLink.CreateAlternateLink(New Uri(str))
    End Function

    Private Function mkRssTxt(ByVal str As String) As SyndicationContent
        Return SyndicationContent.CreatePlaintextContent(str)
    End Function

    Private Function getfullURL(ByVal str As String)
        Return Request.Url.GetLeftPart(UriPartial.Authority) & ResolveUrl(str)
    End Function
End Class